package com.goldplusgold.tradedelay.app.my.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.goldplusgold.live.LVDataManager;
import com.goldplusgold.live.bean.AnswerBean;
import com.goldplusgold.live.bean.SwitchBean;
import com.goldplusgold.live.result.ResultLiveQuestions;
import com.goldplusgold.live.result.ResultUserQuestions;
import com.goldplusgold.live.result.ResultVideoQuestions;
import com.goldplusgold.support.lib.base.activity.BaseActivity;
import com.goldplusgold.support.lib.utils.Logger;
import com.goldplusgold.support.lib.widget.DividerItemDecoration;
import com.goldplusgold.tradedelay.app.R;
import com.goldplusgold.tradedelay.app.base.widget.recyclerview.EndlessRecyclerOnScrollListener;
import com.goldplusgold.tradedelay.app.home.live.adapter.AnswerAdapter;
import com.goldplusgold.tradedelay.app.home.live.base.LiveBaseCallBack;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/7/24.
 */

public class QuestionsActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.title_txt)
    TextView titleView;
    @BindView(R.id.answers_recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.hint_layout)
    LinearLayout hint_layout;
    AnswerAdapter answerViewAdapter;
    int offset = 0;
    SwitchBean bean;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_questions;
    }

    @Override
    protected void initView() {
        super.initView();
        iv_back.setVisibility(View.VISIBLE);
        titleView.setVisibility(View.VISIBLE);
        titleView.setText(R.string.my_question);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mRecyclerView.getContext());
        mRecyclerView.setLayoutManager(linearLayoutManager);
        answerViewAdapter = new AnswerAdapter(this);
        mRecyclerView.setAdapter(answerViewAdapter);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,
                DividerItemDecoration.VERTICAL_LIST));
        mRecyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) {
            @Override
            public void onLoadMore(int currentPage) {
                offset++;
                requestData(bean);
            }
        });
        requestSwitch();
//        requestData();
    }

    private void requestSwitch(){
        //判断其是否显示
        LVDataManager.getInstance().getSwitch(new Callback<SwitchBean>() {
            @Override
            public void onResponse(Call<SwitchBean> call, Response<SwitchBean> response) {
                bean = response.body();
                requestData(bean);
            }

            @Override
            public void onFailure(Call<SwitchBean> call, Throwable t) {
                Logger.e("requestSwitch",t.toString());
            }
        });
    }

    private void requestData(SwitchBean bean){
        if(bean.isColumnFlag() && bean.isVideoFlag()){
            LVDataManager.getInstance().getUserQuestions(offset, 10, new LiveBaseCallBack<ResultUserQuestions>(this) {
                @Override
                public void onResponse(Call<ResultUserQuestions> call, Response<ResultUserQuestions> response) {
                    super.onResponse(call , response);
                    refreshListView(response.body().getDatas());
                }

                @Override
                public void onFailure(Call<ResultUserQuestions> call, Throwable t) {
                    Logger.e("userquestions","failure");
                }
            });
        }
        if(bean.isColumnFlag() && !bean.isVideoFlag()){
            LVDataManager.getInstance().getLiveQuestions("", offset, 10, new LiveBaseCallBack<ResultLiveQuestions>(this) {
                @Override
                public void onResponse(Call<ResultLiveQuestions> call, Response<ResultLiveQuestions> response) {
                    super.onResponse(call , response);
                    refreshListView(response.body().getDatas());
                }

                @Override
                public void onFailure(Call<ResultLiveQuestions> call, Throwable t) {
                    super.onFailure(call , t);
                }
            });
        }
        if(!bean.isColumnFlag() && bean.isVideoFlag()){
            LVDataManager.getInstance().getVideoQuestions("", offset, 10, new LiveBaseCallBack<ResultVideoQuestions>(this) {
                @Override
                public void onResponse(Call<ResultVideoQuestions> call, Response<ResultVideoQuestions> response) {
                    super.onResponse(call , response);
                    refreshListView(response.body().getDatas());
                }

                @Override
                public void onFailure(Call<ResultVideoQuestions> call, Throwable t) {

                }
            });
        }

    }
    private void refreshListView(final List<AnswerBean> datas) {
        if(datas != null && datas.size() > 0){
            hint_layout.setVisibility(View.GONE);
            if(offset == 0) {
                answerViewAdapter.setDatas(datas);
            }else{
                answerViewAdapter.addDatas(datas);
            }
            offset = offset + datas.size() ;
            answerViewAdapter.notifyDataSetChanged();
        }
    }


    @OnClick({R.id.iv_back,})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                this.finish();
                break;
        }
    }
}
